package sub;

/**
 * @program: javase
 * @description:
 * @author: lld
 * @create: 2021-06-08 14:58
 **/
public class SubString {
    public static void main(String[] args) {
        int sub = sub("abc", "abd cxdab cd");
        System.out.println(sub);
    }

    public static int sub(String sub, String str) {
        int subLen = sub.length();
        int strLen = str.length();
        int i = 0, j = 0;
        while (i < strLen && j < subLen) {
            if (str.charAt(i) == sub.charAt(j)) {
                i++;
                j++;
            } else {
                i = i - j + 1;
                j = 0;
            }
            if (j == subLen) {
                return i - j;
            }
        }
        return -1;
    }
}
